package website.seamonster.ethegg.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import website.seamonster.ethegg.dataobject.Account;
import website.seamonster.ethegg.dataobject.User;
import website.seamonster.ethegg.dto.AccountDTO;
import website.seamonster.ethegg.enums.ResultEnum;
import website.seamonster.ethegg.exception.EthException;
import website.seamonster.ethegg.service.AccountService;
import website.seamonster.ethegg.service.EggService;
import website.seamonster.ethegg.service.TxReqService;
import website.seamonster.ethegg.service.UserService;
import website.seamonster.ethegg.utils.ResultVOUtil;
import website.seamonster.ethegg.viewobject.ResultVO;

import java.math.BigDecimal;
import java.util.Objects;

/**
 * Created by 罗海量
 * 2018-07-23 23:16
 */
@RestController
@lombok.extern.slf4j.Slf4j
@RequestMapping("/shop")
public class ShopController {
    @Autowired
    private TxReqService txReqService;
    @Autowired
    private EggService eggService;
    @Autowired
    private UserService userService;
    @Autowired
    private AccountService accountService;

    /**
     * 购买蛋
     * @return
     */
    @PostMapping("/buy")
    public ResultVO buy(@RequestParam("buyCnt") Integer buyCnt,
                        @RequestParam("userId")String userId,
                        @RequestParam(name="prCode", required = false) String prCode,
                        @RequestParam("clientId") String clientId){
        //判断购买的数量是否超过现有出售中的蛋的数量： EggService
        if (!eggService.isEggEnough(buyCnt)){
            throw new EthException(ResultEnum.EGG_NO_ENOUGH);
        }
        //判断购买的数量是否超过该用户孵化圈上限数-鸡的数目 -未处理完成的交易请求记录中的购买数目？)
        if (userService.getFreeHatchNum(userId) - buyCnt - txReqService.getBuyingEggNumsByUserId(userId) < 0){
            throw new EthException(ResultEnum.HATCH_NO_ENOUGH);
        }
        //如果prCode不为空，需判断prCode有效性，并获取该prCode所属用户：UserService
        User prUser = userService.getUserByPrCode(prCode);

        //不可以用自己的推广码买蛋！
        if (prUser!=null && Objects.equals(userId, prUser.getId())){
            throw new EthException(ResultEnum.PRCODE_NOT_VALID);
        }

        //判断是否够钱
        BigDecimal balance = accountService.getBalanceByUser(userId);
        if (balance.compareTo(eggService.getEggPrice(userId).multiply(BigDecimal.valueOf(buyCnt.longValue())))<0){
            throw new EthException(ResultEnum.NO_MONEY);
        }

        //新建一条交易请求
        txReqService.createTx(clientId, userId, prUser, buyCnt);
        return ResultVOUtil.success();
    }

    /**
     * 剩余蛋数，当前蛋价，余额。。。
     * @return
     */
    @GetMapping("/account")
    public ResultVO<AccountDTO> getAccount(@RequestParam("userId")String userId){
        AccountDTO dto = new AccountDTO();
        dto.setEggNum(eggService.getSellingEggNum());
        dto.setPrice(eggService.getEggPrice(userId));
        dto.setBalance(accountService.getBalanceByUser(userId));
        return ResultVOUtil.success(dto);
    }
}
